package com.softwareconstruction.selection;

import com.softwareconstruction.paper.Paper;
import com.softwareconstruction.student.Student;

import java.util.*;

/**
 * 论文选择结果类，使用Visitor模式实现统计功能
 */
public class PaperSelection {
    private final Map<Paper, Student> selections;

    public PaperSelection() {
        selections = new HashMap<>();
    }

    public boolean addSelection(Student student, Paper paper) {
        if (selections.containsKey(paper)) {
            return false;
        }
        selections.put(paper, student);
        return true;
    }

    public boolean isSelected(Paper paper) {
        return selections.containsKey(paper);
    }

    public Map<Paper, Student> getSelections() {
        return new HashMap<>(selections);
    }

    public void accept(Visitor visitor) {
        for (Map.Entry<Paper, Student> entry : selections.entrySet()) {
            visitor.visit(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        for (Map.Entry<Paper, Student> entry : selections.entrySet()) {
            result.append(entry.getValue().getName())
                    .append(" 选择了: ")
                    .append(entry.getKey().getTitle())
                    .append("\n");
        }
        return result.toString();
    }
}